/*
 * GotoDialog.java
 *
 * Created on July 15, 2007
 *
 * @author Alex Riedler (Ultrahex)
 *
 * @description
 *      Goto Line Dialog, Created by MainFrame (Main Window)
 *           
 */


package ca.compsci.opent.ide;

import javax.swing.*;
import javax.swing.text.*;

/**
 *
 * @author  Ultrahex
 */
public class GotoDialog extends javax.swing.JFrame {
    public JTextPane textArea;
    /**
     * Creates new form GotoDialog
     */
    public GotoDialog(JTextPane textArea) {
        this.textArea=textArea;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lineNumberLabel = new javax.swing.JLabel();
        lineNumberField = new javax.swing.JTextField();
        gotoButton = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Goto...");
        lineNumberLabel.setText("Line:");

        gotoButton.setText("Goto");
        gotoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gotoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lineNumberLabel)
                .addGap(23, 23, 23)
                .addComponent(lineNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addComponent(gotoButton)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lineNumberLabel)
                    .addComponent(lineNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(gotoButton))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void gotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gotoActionPerformed
        Element root = textArea.getDocument().getDefaultRootElement();
        try {
            int line = Integer.parseInt(lineNumberField.getText());
            line = Math.max(line, 1);
            line = Math.min(line, root.getElementCount());
            textArea.setCaretPosition( root.getElement( line - 1 ).getStartOffset() );
        } catch (Exception e) {
        }
        dispose();
    }//GEN-LAST:event_gotoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton gotoButton;
    private javax.swing.JTextField lineNumberField;
    private javax.swing.JLabel lineNumberLabel;
    // End of variables declaration//GEN-END:variables
    
}
